package com.fcgzs.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.fcgzs.entity.Result;
import com.fcgzs.item.service.PageService;
import com.fcgzs.sellergoods.feign.GoodsFeign;
import com.fcgzs.sellergoods.feign.ItemCatFeign;
import com.fcgzs.sellergoods.group.GoodsEntity;
import com.fcgzs.sellergoods.pojo.Goods;
import com.fcgzs.sellergoods.pojo.GoodsDesc;
import com.fcgzs.sellergoods.pojo.Item;
import com.fcgzs.sellergoods.pojo.ItemCat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: ljj
 * @Description:
 */
@Service
public class PageServiceImpl implements PageService {

    @Autowired
    private GoodsFeign goodsFeign;

    @Autowired
    private ItemCatFeign itemCatFeign;

    @Value("${pagepath}")
    private String pagepath;

    @Autowired
    private TemplateEngine templateEngine;

    private Map<String, Object> buildDataModel(Long goodsId) {
        Map<String, Object> resultMap = new HashMap<>();

        Result<GoodsEntity> goodsResult = goodsFeign.findById(goodsId);
        GoodsEntity goodsEntity = goodsResult.getData();
        //查询SPU信息
        Goods goods = goodsEntity.getGoods();
        //查询扩展信息
        GoodsDesc goodsDesc = goodsEntity.getGoodsDesc();
        //查询SKU信息
        List<Item> itemList = goodsEntity.getItemList();
        //查询分类信息
        Result<ItemCat> itemCat1Result = itemCatFeign.findById(goods.getCategory1Id());
        ItemCat itemCat1 = itemCat1Result.getData();

        Result<ItemCat> itemCat2Result = itemCatFeign.findById(goods.getCategory2Id());
        ItemCat itemCat2 = itemCat2Result.getData();


        Result<ItemCat> itemCat3Result = itemCatFeign.findById(goods.getCategory3Id());
        ItemCat itemCat3 = itemCat3Result.getData();

        resultMap.put("goods", goods);
        resultMap.put("goodsDesc", goodsDesc);
        resultMap.put("imageList", JSON.parseArray(goodsDesc.getItemImages(),Map.class));
        resultMap.put("specList",JSON.parseArray(goodsDesc.getSpecificationItems(),Map.class));

        resultMap.put("itemList", itemList);
        resultMap.put("category1", itemCat1);
        resultMap.put("category2", itemCat2);
        resultMap.put("category3", itemCat3);
        return resultMap;

    }


    /**
     * 生成静态页面
     *
     * @param goodsId
     * @return
     */
    @Override
    public boolean createItemHtml(Long goodsId) {
        try {
            //1.获取生成模板的数据信息
            Map<String, Object> dataModel = this.buildDataModel(goodsId);

            //2.创建上下文对象，通过上下文与模板之间进行数据交互
            Context context = new Context();
            context.setVariables(dataModel);
            //3.创建存放模板的目录信息
            File dir = new File(pagepath);
            if (null == dir) {
                dir.mkdirs();   //初始化目录
            }

            //4.通过模板引擎对象完成生成HTML
            File file = new File(dir, goodsId + ".html");
            //5.通过输出流保存HTML文件
            PrintWriter out = new PrintWriter(file, "UTF-8");
            templateEngine.process("item", context, out);

            out.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }
}
